package cn.edu.nju.oraldefense.action;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;

import cn.edu.nju.oraldefense.model.Dean;
import cn.edu.nju.oraldefense.model.Graduate;
import cn.edu.nju.oraldefense.model.Observer;
import cn.edu.nju.oraldefense.model.Teacher;
import cn.edu.nju.oraldefense.service.DeanService;
import cn.edu.nju.oraldefense.service.GraduateService;
import cn.edu.nju.oraldefense.service.ObserverService;
import cn.edu.nju.oraldefense.service.TeacherService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private String username;
	private String password;
	private String type;
	private GraduateService graduateService;
	private TeacherService teacherService;
	private DeanService deanService;
	private ObserverService observerService;
	private boolean success = false;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@JSON(serialize = false)
	public GraduateService getGraduateService() {
		return graduateService;
	}

	@Resource
	public void setGraduateService(GraduateService graduateService) {
		this.graduateService = graduateService;
	}

	@JSON(serialize = false)
	public TeacherService getTeacherService() {
		return teacherService;
	}

	@Resource
	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	@JSON(serialize = false)
	public DeanService getDeanService() {
		return deanService;
	}

	@Resource
	public void setDeanService(DeanService deanService) {
		this.deanService = deanService;
	}

	@JSON(serialize = false)
	public ObserverService getObserverService() {
		return observerService;
	}

	@Resource
	public void setObserverService(ObserverService observerService) {
		this.observerService = observerService;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String execute() {
		System.out.println(password);
		System.out.println(type);

		ActionContext ctx = ActionContext.getContext();
		if (type.equals("dean")) {
			Dean instance = deanService.getByUsername(username);
			if (instance != null && instance.getPassword().equals(password)) {
				ctx.getSession().put(type, instance);
				success = true;
			}
		}
		if (type.equals("graduate")) {
			Graduate instance = graduateService.getByNumber(username);
			if (instance != null && instance.getPassword().equals(password)) {
				ctx.getSession().put(type, instance);
				success = true;
			}
		}
		if (type.equals("teacher")) {
			Teacher instance = teacherService.getByNumber(username);
			if (instance != null && instance.getPassword().equals(password)) {
				ctx.getSession().put(type, instance);
				success = true;
			}
		}
		if (type.equals("observer")) {
			Observer instance = observerService.getByUsername(username);
			if (instance != null && instance.getPassword().equals(password)) {
				ctx.getSession().put(type, instance);
				success = true;
			}
		}
		return SUCCESS;
	}
}
